package validate;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.appengine.api.datastore.Email;
import com.google.appengine.api.datastore.PhoneNumber;


public class Validate {
	
	static Pattern pattern;
	static Matcher matcher;
	//Initialize reg ex for email. 
	static String emailExpression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";  
	//Initialize reg ex for phone number.   
	static String phoneExpression = "(\\d{2,3})(-)?(\\d{7})";
	
	static public boolean emailValid(String email)
	{		
		//Make the comparison case-insensitive.  
		pattern = Pattern.compile(emailExpression,Pattern.CASE_INSENSITIVE);  
		matcher = pattern.matcher(email);  
		
		if(!matcher.matches())
		{
			System.out.println("email not valid");
			return false;
		}
		
		return true;
	}
	
	
	static public boolean phoneValid(String phone)
	{
		pattern = Pattern.compile(phoneExpression);  
		matcher = pattern.matcher(phone);  
		if (!matcher.matches())
		{
			System.out.println("phone not valid");
			return false;
		}
		
		return true;
	}
	
	/**
	 * 
	 * @param businessName
	 * @param businessNumber
	 * @param imageBytes
	 * @param phone
	 * @param email
	 * @param userName
	 * @param password
	 * @param password1
	 * @param status
	 * @param EtaList
	 * @param message
	 * @return 0 - success, 1- passwords not match, 2 - email not valid, 3 - phone not valid, 4 - empty field
	 */
	static public String checkBizValid(String businessName, String businessNumber, byte[] imageBytes, PhoneNumber phone, Email email, 
			String userName, String password, String password1, ArrayList<String> status, ArrayList<Double> EtaList, ArrayList<String> message)
	{
		if (!password.equals(password1))
		{
			System.out.println("passwords not match");
			return "confirm password is not match to your password";
		}
		
		if (!Validate.emailValid(email.getEmail()))
			return "invalid email format";
		
	
		if (!Validate.phoneValid(phone.getNumber()))
			return "invalid phone format";
		
		if (businessName.equals(""))
				System.out.println("empty businessName");
		
		if (businessNumber.equals(""))
			System.out.println("empty businessNumber");
		
		if (userName.equals(""))
			System.out.println("empty userName");
		
		if (status.isEmpty())
			System.out.println("empty status");
		
		if (businessName.equals("")||businessNumber.equals("")||userName.equals("")||status.isEmpty())
		{
			System.out.println("empty field");
			return "you didn't fill all required fields";
		}
			
		return "";
		
	}
	
	static public String checkClientValid(String userName, PhoneNumber phone, Email email, String password, String password1)
	{
		if (!password.equals(password1))
		{
			System.out.println("passwords not match");
			return "confirm password is not match to your password";
		}
		
		if (password.length()==0)
		{
			System.out.println("password is empty string");
			return "password can not be empty";
		}
		
		if (!Validate.emailValid(email.getEmail()))
			return "invalid email format";
		
	
		if (!Validate.phoneValid(phone.getNumber()))
			return "invalid phone format";
		
		if (userName.equals(""))
		{
			System.out.println("empty field");
			return "you didn't fill all required fields";
		}
			
		return "";
		
	}

}
